package com.crl.ssc.dic.controller;

import com.cars.util.bean.BasePage;
import com.cars.util.global.GlobalOperaCode;
import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.string.StringUtil;
import com.cars.util.validation.UpdateValidGroup;
import com.crl.ssc.dic.dto.DicProvinceDTO;
import com.crl.ssc.dic.service.DicProvinceService;
import com.crl.ssc.dic.vo.DicProvinceSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @description:省份字典管理
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/21 13:58
 */
@RestController
@RequestMapping("/dic/province")
@Api(tags = "字典管理-省份字典管理")
public class DicProvinceController {

    @Autowired
    private DicProvinceService dicProvinceService;

    /**
     * 省份字典列表
     *
     * @param dicProvinceSearchVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "省份字典列表")
    public JsonResult list(@RequestBody DicProvinceSearchVO dicProvinceSearchVO) {
        int count = dicProvinceService.count(dicProvinceSearchVO);
        List<DicProvinceDTO> list = dicProvinceService.list(dicProvinceSearchVO);
        BasePage<List<DicProvinceDTO>> basePage = new BasePage(count, list);
        return JsonResultUtil.ok(basePage);
    }


    /**
     * 新增省份字典
     *
     * @param dicProvinceDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增省份字典")
    public JsonResult add(@RequestBody @Valid DicProvinceDTO dicProvinceDTO) {
        int flag = dicProvinceService.add(dicProvinceDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 修改省份字典
     *
     * @param dicProvinceDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改省份字典")
    public JsonResult update(@Validated({UpdateValidGroup.class}) @RequestBody DicProvinceDTO dicProvinceDTO) {
        int flag = dicProvinceService.update(dicProvinceDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }

    /**
     * 删除省份字典
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除省份字典")
    public JsonResult delete(@RequestBody Map<String, String> map) {
        String provinceCode = map.get("provinceCode");
        if (StringUtil.isNullOrEmpty(provinceCode)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        int flag = dicProvinceService.delete(provinceCode);
        if (flag == GlobalOperaCode.OPERA_SUCCESS) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }


    /**
     * 获取单个省份字典
     *
     * @param map
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个省份字典")
    public JsonResult get(@RequestBody Map<String, String> map) {
        String provinceCode = map.get("provinceCode");
        if (StringUtil.isNullOrEmpty(provinceCode)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        DicProvinceDTO dicProvinceDTO = dicProvinceService.get(provinceCode);
        return JsonResultUtil.ok(dicProvinceDTO);
    }
}
